package com.irdstudio.efp.cus.service.impl;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.util.DateUtility;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.efp.cus.service.dao.PersonalWhiteListDao;
import com.irdstudio.efp.cus.service.domain.PersonalWhiteList;
import com.irdstudio.efp.cus.service.facade.PersonalWhiteListService;
import com.irdstudio.efp.cus.service.vo.PersonalWhiteDeleteExcelVO;
import com.irdstudio.efp.cus.service.vo.PersonalWhiteListVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.irdstudio.basic.framework.core.util.StringUtil;

import java.util.List;
import java.util.Objects;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

/**
 * Service Impl: 服务实现
 *
 * @author gcb
 * @date 2020-07-31
 */
@Service("personalWhiteListService")
public class PersonalWhiteListServiceImpl implements PersonalWhiteListService, FrameworkService {

    private static Logger logger = LoggerFactory.getLogger(PersonalWhiteListServiceImpl.class);

    @Autowired
    private PersonalWhiteListDao personalWhiteListDao;

    /**
     * 新增数据
     *
     * @param personalWhiteListVo
     * @return
     */
    @Override
    public int insert(PersonalWhiteListVO personalWhiteListVo) {
        logger.debug("当前新增数据为:" + personalWhiteListVo.toString());
        int num = 0;
        try {
            PersonalWhiteList personalWhiteList = new PersonalWhiteList();
            beanCopy(personalWhiteListVo, personalWhiteList);
            num = personalWhiteListDao.insert(personalWhiteList);
        } catch (Exception e) {
            logger.error("新增数据发生异常!!", e);
            num = -1;
        }
        logger.debug("当前新增数据条数为:" + num);
        return num;
    }

    /**
     * 根据主键删除信息
     *
     * @param personalWhiteListVo
     * @return
     */
    @Override
    public int deleteByPk(PersonalWhiteListVO personalWhiteListVo) {
        logger.debug("当前删除数据条件为:" + personalWhiteListVo);
        int num = 0;
        try {
            PersonalWhiteList personalWhiteList = new PersonalWhiteList();
            beanCopy(personalWhiteListVo, personalWhiteList);
            num = personalWhiteListDao.deleteByPk(personalWhiteList);
        } catch (Exception e) {
            logger.error("删除数据发生异常!", e);
            num = -1;
        }
        logger.debug("根据条件:" + personalWhiteListVo + "删除的数据条数为" + num);
        return num;
    }

    /**
     * 根据主键更新信息
     *
     * @param personalWhiteListVo
     * @return
     */
    @Override
    public int updateByPk(PersonalWhiteListVO personalWhiteListVo) {
        logger.debug("当前修改数据为:" + personalWhiteListVo.toString());
        int num = 0;
        try {
            PersonalWhiteList personalWhiteList = new PersonalWhiteList();
            beanCopy(personalWhiteListVo, personalWhiteList);
            num = personalWhiteListDao.updateByPk(personalWhiteList);
        } catch (Exception e) {
            logger.error("修改数据发生异常!", e);
            num = -1;
        }
        logger.debug("根据条件:" + personalWhiteListVo + "修改的数据条数为" + num);
        return num;
    }

    /**
     * 根据主键查询详情
     *
     * @param inPersonalWhiteListVo
     * @return
     */
    @Override
    public PersonalWhiteListVO queryByPk(PersonalWhiteListVO inPersonalWhiteListVo) {
        logger.debug("当前查询参数信息为:" + inPersonalWhiteListVo);
        try {
            PersonalWhiteList queryPersonalWhiteList = new PersonalWhiteList();
            beanCopy(inPersonalWhiteListVo, queryPersonalWhiteList);
            PersonalWhiteList queryRslPersonalWhiteList = personalWhiteListDao.queryByPk(queryPersonalWhiteList);
            if (Objects.nonNull(queryRslPersonalWhiteList)) {
                return beanCopy(queryRslPersonalWhiteList, new PersonalWhiteListVO());
            } else {
                logger.debug("当前查询结果为空!");
            }
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
        }
        return null;
    }

    @Override
    public List<PersonalWhiteListVO> queryPWhiteListByCondition(PersonalWhiteListVO personalWhiteListVO) {
        logger.debug("当前查询参数信息为:" + personalWhiteListVO);
        List<PersonalWhiteListVO> queryRslCompanyWhiteList=null;
        try {
            List<PersonalWhiteList> whiteList = personalWhiteListDao.queryPWhiteListByCondition(personalWhiteListVO);
            queryRslCompanyWhiteList = (List<PersonalWhiteListVO>) beansCopy(whiteList, PersonalWhiteListVO.class);

        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
        }
        return queryRslCompanyWhiteList;

    }

    /**
     * 查询列表数据
     */
    @Override
    public List<PersonalWhiteListVO> queryAllList(PersonalWhiteListVO inPersonalWhiteListVO) {
        List<PersonalWhiteListVO> list = new ArrayList<PersonalWhiteListVO>();

        if(inPersonalWhiteListVO.getIsQuery()) {//只有点击查询的时候校验
            //校验输入参数是否为空
            if (StringUtils.isBlank(inPersonalWhiteListVO.getCertType())
                    && StringUtils.isBlank(inPersonalWhiteListVO.getCusName())
                    && StringUtils.isBlank(inPersonalWhiteListVO.getCertCode())
                    && StringUtils.isBlank(inPersonalWhiteListVO.getCompanyName())
                    && StringUtils.isBlank(inPersonalWhiteListVO.getImportEmpName())
                    && StringUtils.isBlank(inPersonalWhiteListVO.getUpdateName())
                    && (StringUtils.isBlank(inPersonalWhiteListVO.getUpdateStrDt()) || StringUtils.isBlank(inPersonalWhiteListVO.getUpdateEndDt()))
                    && (StringUtils.isBlank(inPersonalWhiteListVO.getImportStrDt()) || StringUtils.isBlank(inPersonalWhiteListVO.getImportEndDt()))
                    && StringUtils.isBlank(inPersonalWhiteListVO.getAppearOrg())
            ) {
                return list;
            }
        }

        PersonalWhiteList personalWhiteList = new PersonalWhiteList();
        beanCopy(inPersonalWhiteListVO, personalWhiteList);
        try {
            List<PersonalWhiteList> personalWhiteLists = personalWhiteListDao.queryAllListByPage(personalWhiteList);
            logger.debug("当前查询本人所属数据信息的结果集数量为:"+ personalWhiteLists.size());

            pageSet(personalWhiteLists,personalWhiteList);

            list = (List<PersonalWhiteListVO>) beansCopy(personalWhiteLists, PersonalWhiteListVO.class);

        } catch (Exception e) {
            logger.error("数据转换出现异常!", e);
        }
        return list;
    }

    @Override
    public int batchDeleteByCode(PersonalWhiteListVO personalWhiteListVO) {
        logger.debug("当前删除数据条件为:"+ personalWhiteListVO);
        int num = 0;
        try {
            List<String> personIdList = null;
            if (Objects.nonNull(personalWhiteListVO) && !StringUtil.isNullorBank(personalWhiteListVO.getCertCode())) {
                personIdList = Arrays.asList(personalWhiteListVO.getCertCode().split(","));
            }
            num = personalWhiteListDao.batchDeleteByCode(personIdList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    @Override
    public int upDateToSetDt(PersonalWhiteListVO personalWhiteListVO){
        logger.debug("当前数据条件为:"+ personalWhiteListVO);
        int num = 0;
        try {
            num = personalWhiteListDao.updateSetDt(personalWhiteListVO);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("更新数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    @Override
    public List<PersonalWhiteListVO> queryConditions(PersonalWhiteListVO personalWhiteListVO) {
        logger.debug("当前查询参数信息为:"+ personalWhiteListVO);
        List<PersonalWhiteListVO> list = null;
        try {
            PersonalWhiteList queryPersonalWhiteList = new PersonalWhiteList();
            beanCopy(personalWhiteListVO, queryPersonalWhiteList);
            List<PersonalWhiteList> resPersonalWhiteList = personalWhiteListDao.queryConditions(queryPersonalWhiteList);
            if (Objects.nonNull(resPersonalWhiteList)&&!resPersonalWhiteList.isEmpty()) {
                List<PersonalWhiteListVO> outPersonalWhiteList = (List<PersonalWhiteListVO>) beansCopy(resPersonalWhiteList, PersonalWhiteListVO.class);
                logger.debug("当前查询结果为:"+ outPersonalWhiteList.toString());
                return outPersonalWhiteList;
            } else {
                logger.debug("当前查询结果为空!");
            }
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
        }
        return null;
    }

    @Override
    public int batchInsertList(List<PersonalWhiteListVO> inserListBatch) {
        logger.debug("当前新增数据条数为:"+ inserListBatch.size());
        int num = 0;
        try {
            num = personalWhiteListDao.batchInsertList(inserListBatch);
        } catch (Exception e) {
            logger.error("新增数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    @Override
    public int updatePersonal(PersonalWhiteListVO updatePersonal){
        logger.debug("当前数据条件为:"+ updatePersonal);
        int num = 0;
        try {
            PersonalWhiteList queryPersonalWhiteParam = new PersonalWhiteList();
            beanCopy(updatePersonal, queryPersonalWhiteParam);
            num = personalWhiteListDao.updatePeronalWhite(queryPersonalWhiteParam);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("更新数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    @Override
    public int batchDelPersnalWhiteByExcel(List<PersonalWhiteDeleteExcelVO>  personalWhiteDelExcelVO){
        logger.debug("当前新增数据条数为:"+ personalWhiteDelExcelVO.size());
        int num = 0;
        try {
            num = personalWhiteListDao.batchDelPersnalWhiteByExcel(personalWhiteDelExcelVO);
        } catch (Exception e) {
            logger.error("新增数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    @Override
    public int effectiveDate(){
        int num = 0;
        try {
            num = personalWhiteListDao.effectiveDate();
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
            num = -1;
        }
        return num;
    }
}
